package com.cncf.service.impl;

import com.cncf.dao.EnterpriseMapper;
import com.cncf.entity.Enterprise;
import com.cncf.entity.EnterpriseExample;
import com.cncf.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author zj 
*/
@Service
public class EnterpriseServiceImpl implements EnterpriseService {
    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Override
    public int addEnterprise(Enterprise enterprise){
        return enterpriseMapper.insert(enterprise);
    }

    @Override
    public int updateEnterprise(Enterprise enterprise){
        return enterpriseMapper.updateByPrimaryKeySelective(enterprise);
    }

    @Override
    public Enterprise getEnterpriseById(String id){
        return enterpriseMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Enterprise> getEnterpriseByName(String name) {
        EnterpriseExample enterpriseExample=new EnterpriseExample();
        EnterpriseExample.Criteria criteria=enterpriseExample.createCriteria();
        criteria.andEnterpriseNameEqualTo(name);
        List<Enterprise> enterpriseList=enterpriseMapper.selectByExample(enterpriseExample);
        return enterpriseList;
    }

    @Override
    public List<Enterprise> getAllEnterprise(Integer page, Integer pageSize){
        if (page==null||pageSize==null){
            EnterpriseExample enterpriseExample=new EnterpriseExample();
            return enterpriseMapper.selectByExample(enterpriseExample);
        }
        int offset = page*pageSize;
        int limit = pageSize;
        return enterpriseMapper.getAllEnterpriseByPage(offset,limit);
    }

    @Override
    public List<Enterprise> getAllNewEnterprise(Integer page, Integer pageSize) {
        if (page==null||pageSize==null){
            return enterpriseMapper.getAllNewEnterprise();
        }
        int offset = page*pageSize;
        int limit = pageSize;
        return enterpriseMapper.getAllNewEnterpriseByPage(offset,limit);
    }

    @Override
    public List<Enterprise> getEnterpriseByIdList(List<String> idList) {
        EnterpriseExample enterpriseExample=new EnterpriseExample();
        EnterpriseExample.Criteria criteria=enterpriseExample.createCriteria();
        criteria.andIdIn(idList);
        List<Enterprise> enterpriseList=enterpriseMapper.selectByExample(enterpriseExample);
        return enterpriseList;
    }

    @Override
    public int deleteEnterprise(String id){
        return enterpriseMapper.deleteByPrimaryKey(id);
    }
}